package com.market.service.wx.impl;

import com.market.bean.*;
import com.market.bean.vo.wx.IndexFloorGoodsVo;
import com.market.bean.vo.wx.WxHomeVo;
import com.market.mapper.*;
import com.market.service.wx.WxHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author fan
 * @Description:
 * @since 2023/01/02 21:43
 */
@Service
public class WxHomeServiceImpl implements WxHomeService {
    @Autowired
    MarketAdMapper adMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketSystemMapper systemMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketTopicMapper topicMapper;



    @Override
    public WxHomeVo index() {
    //TODO    private List<MarketGoods> newGoodsList;
    //    private List<MarketCoupon> couponList;
    //    private List<MarketCategory> channel;
    //    private List<MarketAd> banner;
    //    private List<MarketBrand> brandList;
    //    private List<MarketGoods> hotGoodsList;
    //    private List<MarketTopic> topicList;
    //    private List<IndexFloorGoodsVo> floorGoodsList;

        WxHomeVo data = new WxHomeVo();
        Integer keyValue = Integer.valueOf(systemMapper.selectByKeyName("market_wx_index_new"));
        Random random = new Random();


        //List<MarketAd> banner;
        MarketAdExample marketAdExample = new MarketAdExample();
        marketAdExample.createCriteria().andDeletedEqualTo(false).andPositionEqualTo(((byte) 1));
        data.setBanner(adMapper.selectByExample(marketAdExample));

        //List<MarketBrand> brandList;
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria().andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(marketBrandExample);
        List<MarketBrand> marketBrands1 = new ArrayList<>();
        if (marketBrands.size() > keyValue) {
            for (int i = 0; i < keyValue; i++) {
                marketBrands1.add(marketBrands.remove(random.nextInt(marketBrands.size())));
            }
        } else {
            marketBrands1 = marketBrands;
        }
        data.setBrandList(marketBrands1);

        //List<MarketCategory> channel;
        MarketCategoryExample marketCategoryExample= new MarketCategoryExample();
        marketCategoryExample.createCriteria().andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        List<MarketCategory> marketCategories1 = new ArrayList<>();
        if (marketBrands.size() > keyValue) {
            for (int i = 0; i < keyValue; i++) {
                marketCategories1.add(marketCategories.remove(random.nextInt(marketCategories.size())));
            }
        } else {
            marketCategories1 = marketCategories;
        }
        data.setChannel(marketCategories1);

        //List<MarketCoupon> couponList;
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.createCriteria().andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
        List<MarketCoupon> marketCoupons1 = new ArrayList<>();
        if (marketBrands.size() > keyValue) {
            for (int i = 0; i < 3; i++) {
                marketCoupons1.add(marketCoupons.remove(random.nextInt(marketCoupons.size())));
            }

        } else {
            marketCoupons1 = marketCoupons;
        }
        data.setCouponList(marketCoupons1);

        List<IndexFloorGoodsVo> indexFloorGoodsVos = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories1) {
            Integer id = marketCategory.getId();
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            marketGoodsExample.createCriteria().andCategoryIdEqualTo(id).andDeletedEqualTo(false);
            List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
            indexFloorGoodsVos.add(new IndexFloorGoodsVo(marketGoods, id, marketCategory.getName()));
        }
        data.setFloorGoodsList(indexFloorGoodsVos);

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andDeletedEqualTo(false).andIsHotEqualTo(true);
        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        List<MarketGoods> marketGoods1 = new ArrayList<>();
        if (marketGoods.size() > keyValue) {
            for (int i = 0; i < keyValue; i++) {
                marketGoods1.add(marketGoods.remove(random.nextInt(marketGoods.size())));
            }
        } else {
            marketGoods1 = marketGoods;
        }
        data.setHotGoodsList(marketGoods1);

        MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
        marketGoodsExample1.createCriteria().andDeletedEqualTo(false).andIsNewEqualTo(true);
        List<MarketGoods> marketGoods2 = goodsMapper.selectByExample(marketGoodsExample1);
        List<MarketGoods> marketGoods3 = new ArrayList<>();
        if (marketBrands.size() > keyValue) {
            for (int i = 0; i < keyValue; i++) {
                marketGoods3.add(marketGoods2.remove(random.nextInt(marketGoods2.size())));
            }
        } else {
            marketGoods3 = marketGoods2;
        }
        data.setNewGoodsList(marketGoods3);

        MarketTopicExample example4 = new MarketTopicExample();
        example4.createCriteria().andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = topicMapper.selectByExample(example4);
        List<MarketTopic> marketTopics1 = new ArrayList<>();
        if (marketBrands.size() > keyValue) {
            for (int i = 0; i < keyValue; i++) {
                marketTopics1.add(marketTopics.remove(random.nextInt(marketTopics.size())));
            }
        } else {
            marketTopics1 = marketTopics;
        }
        data.setTopicList(marketTopics1);
        return data;
    }
}
